//@ts-nocheck
import {addEvent} from './event';

export function onlyOne(obj){
  return Array.isArray(obj) ? obj[0] : obj;
}

export function setProps(dom, props){
  for (const key in props) {
    if(key != 'children'){
      const value = props[key];
      setProp(dom, key, value)
    }
  }
}

function setProp(dom, key, value){
  if(/^on/.test(key)){
    // dom[key.toLowerCase()] = value; // react使用的是合成事件
    addEvent(dom, key, value);
  }else if (key === 'style'){
    for (const styleName in value){
      dom.style[styleName] = value[styleName];
    }
  }else {
    dom.setAttribute(key, value);
  }
}

// 打平一个任意多维数组
export function flatten(array){
  const flatted = [];
  (function flat(array) {
    array.forEach(item => {
      if (Array.isArray(item)) {
        flat(array)
      } else {
        flatted.push(item)
      }
    });
  })(array);
  return flatted;
}
